package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminRoleCreateVo;
import com.cskaoyan.bean.vo.AdminRolePermissionsVo;
import com.cskaoyan.bean.vo.admin.role.permissions.ParentType;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.utils.DateToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 角色管理
 * @Author: JianhuaZeng
 * @Date: 2022/12/28 21:59
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * @description: 查询所有role
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @GetMapping("list")
    public BaseRespVo<CommonData<MarketRole>> list(BasePageInfo pageInfo){
        CommonData<MarketRole> commonData = roleService.list(pageInfo);
        return BaseRespVo.ok(commonData,"成功");
    }
    
    /**
     * @description: 查询所有的role
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @GetMapping("options")
    public BaseRespVo options(){
        // 查询所有role
        List<MarketRole> roles = roleService.allRole();

        // 构造返回对象
        CommonData<Map<Object,Object>> commonData = new CommonData<>();
        commonData.setLimit(roles.size());
        commonData.setPage(1);
        commonData.setPages(1);
        commonData.setTotal(roles.size());

        // 将roles转存到maps中
        List<Map<Object, Object>> maps = new ArrayList<>();
        roles.forEach(role -> {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("value",role.getId());
            map.put("label",role.getName());
            maps.add(map);
        });
        commonData.setList(maps);
        return BaseRespVo.ok(commonData,"成功");
    }

    /**
     * @description: 创建role数据
     * @param: map
     * @return: com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.AdminRoleCreateVo>
     * @author: JianhuaZeng
     * @date: 2022/12/29 2022/12/29
     */
    @PostMapping("create")
    public BaseRespVo<AdminRoleCreateVo> create(@RequestBody Map map){
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        Date date = new Date();

        // 构造新增对象
        MarketRole role = new MarketRole();
        role.setName(name);
        role.setDesc(desc);
        role.setAddTime(date);
        role.setUpdateTime(date);
        role.setEnabled(true);
        Integer id = roleService.createRole(role);

        // 构造返回对象
        String time = DateToString.getDateString(date);
        AdminRoleCreateVo adminRoleCreateVo = new AdminRoleCreateVo(id,name,desc,time,time);
        return BaseRespVo.ok(adminRoleCreateVo,"成功");
    }

    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId){
        AdminRolePermissionsVo permissionsVo = new AdminRolePermissionsVo();
        List<String> rolePermissions = roleService.getRolePermissions(roleId);
        permissionsVo.setAssignedPermissions(rolePermissions);
        List<ParentType> sysPermissions = roleService.getSysPermissions();
        permissionsVo.setSystemPermissions(sysPermissions);
        return BaseRespVo.ok(permissionsVo,"成功");
    }

    /**
     * @description: 查询role的权限和所有的系统权限
     * @param: map
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/30 2022/12/30
     */
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody Map map){
        Integer roleId = (Integer) map.get("roleId");
        List<String> permissions = (List<String>) map.get("permissions");
        roleService.updatePermissionByRoleId(roleId,permissions);
        return BaseRespVo.ok("成功");
    }

    /**
     * @description: 修改role信息
     * @param: marketRole
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/30 2022/12/30
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketRole marketRole){
        roleService.update(marketRole);
        return BaseRespVo.ok("成功");
    }

    /**
     * @description: 逻辑删除role
     * @param: marketRole
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2022/12/30 2022/12/30
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole marketRole){
        int code = roleService.delete(marketRole);
        // code 0:当前角色存在管理员，不能删除 1:逻辑删除成功
        if (code == 0){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrmsg("当前角色存在管理员，不能删除");
            baseRespVo.setErrno(642);
            return baseRespVo;
        }
        return BaseRespVo.ok("成功");
    }
}
